class ArchitecturesController < ApplicationController
  before_filter :authorize, :logevent
  layout "home"
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @architecture_pages, @architectures = paginate :architectures, :per_page => 10
  end

  def show
    @architecture = Architecture.find(params[:id])
    @layers = Layer.find(:all, :conditions => ["architecture_id = ?", params[:id]])
  end

  def new
    @architecture = Architecture.new
  end

  def create
    @architecture = Architecture.new(params[:architecture])
    @architecture.user_id = session[:user_id]
    if @architecture.save
      flash[:notice] = 'Architecture was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @architecture = Architecture.find(params[:id])
    @layers = Layer.find(:all, :conditions => ["architecture_id = ?", params[:id]])
  end

  def update
    @architecture = Architecture.find(params[:id])
    if @architecture.update_attributes(params[:architecture])
      flash[:notice] = 'Architecture was successfully updated.'
      redirect_to :action => 'show', :id => @architecture
    else
      render :action => 'edit'
    end
  end

  def destroy
    Architecture.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def add_layer
    @layer = Layer.new(params[:layer])
    @layer.architecture_id = params[:id]
    if @layer.save
      flash[:notice] = 'Layer was successfully created.'
    else
      flash[:notice] = 'Layer creation failed.'
    end

    @layers = Layer.find(:all, :conditions => ["architecture_id = ?", params[:id]])
    render(:partial => 'layer', :object => @layer, :layout => false )
  end
  
  def remove_layer
    Layer.find(params[:layer_id]).destroy
    @layers = Layer.find(:all, :conditions => ["architecture_id = ?", params[:id]])

    redirect_to :action => 'show', :id => params[:id]
  end

end
